<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <nav class="navbar">
      <div class="logo">
        <i class="icon-music"></i>
        <span>云音视频</span>
      </div>
      <div class="nav-links">
        <a href="#">发现音乐</a>
        <a href="#">我的音乐</a>
        <a href="#" class="active">音视频</a>
        <a href="#">朋友</a>
        <a href="#">商城</a>
      </div>
    </nav>

    <!-- 主容器 -->
    <div class="container">
      <!-- 搜索区域 -->
      <div class="search-container">
        <div class="search-box">
          <input
              type="text"
              class="search-input"
              placeholder="搜索歌曲、歌手、专辑或视频..."
              v-model="searchTerm"
              @keypress.enter="performSearch"
          >
          <button class="search-button" @click="performSearch">
            <i class="icon-search"></i>
          </button>
        </div>
        <div class="filter-tabs">
          <button
              class="filter-tab"
              :class="{ active: currentMediaType === 'all' }"
              @click="setMediaType('all')"
          >
            全部
          </button>
          <button
              class="filter-tab"
              :class="{ active: currentMediaType === 'audio' }"
              @click="setMediaType('audio')"
          >
            音频
          </button>
          <button
              class="filter-tab"
              :class="{ active: currentMediaType === 'video' }"
              @click="setMediaType('video')"
          >
            视频
          </button>
        </div>
      </div>

      <!-- 列表区域 -->
      <div class="content-title">
        <h1>音视频列表</h1>
      </div>

      <div class="media-list">
        <div class="list-header">
          <div class="col-number">序号</div>
          <div class="col-title">标题</div>
          <div class="col-duration">时长</div>
          <div class="col-type">类型</div>
        </div>

        <!-- 列表项 -->
        <div
            class="list-item"
            v-for="(item, index) in currentItems"
            :key="item.id"
            :data-id="item.id"
            :data-type="item.type"
            @click="openPlayer(item)"
        >
          <div class="col-number item-number" :class="{ top: (currentPage - 1) * itemsPerPage + index + 1 <= 3 }">
            {{ (currentPage - 1) * itemsPerPage + index + 1 }}
          </div>
          <div class="col-title item-info">
            <img
                :src="item.thumbnail"
                :alt="item.title"
                class="media-thumbnail"
            >
            <div class="media-details">
              <div class="media-title">{{ item.title }}</div>
              <div class="media-author">{{ item.author }}</div>
            </div>
          </div>
          <div class="col-duration item-duration">{{ item.duration }}</div>
          <div class="col-type item-type" :class="item.type">
            <i :class="item.type === 'audio' ? 'icon-music' : 'icon-video'"></i>
            {{ item.type === 'audio' ? '音频' : '视频' }}
          </div>
        </div>
      </div>

      <!-- 分页控件 -->
      <div class="pagination">
        <button
            class="page-button prev"
            :class="{ disabled: currentPage === 1 }"
            @click="prevPage"
        >
          <i class="icon-prev"></i>
        </button>
        <span class="page-info">
          第 {{ currentPage }} 页，共 {{ totalPages }} 页
        </span>
        <button
            class="page-button next"
            :class="{ disabled: currentPage === totalPages }"
            @click="nextPage"
        >
          <i class="icon-next"></i>
        </button>
      </div>
    </div>

    <!-- 播放页面 -->
    <div class="player-container" :class="{ active: isPlayerOpen }">
      <button class="close-player" @click="closePlayer">
        <i class="icon-close"></i>
      </button>
      <div class="player-content">
        <div class="player-header">
          <img
              :src="currentMedia.thumbnail"
              :alt="currentMedia.title"
              class="player-thumbnail"
          >
          <div class="player-info">
            <h2>{{ currentMedia.title }}</h2>
            <p>{{ currentMedia.author }}</p>
          </div>
        </div>

        <div id="mediaPlayer">
          <!-- 音频播放器 - 使用原生audio标签 -->
          <div class="audio-player" v-if="currentMedia.type === 'audio'">
            <audio
                :src="currentMedia.url"
                controls
                class="native-audio-player"
                ref="audioElement"
            >
              您的浏览器不支持音频播放
            </audio>
          </div>

          <!-- 视频播放器 - 使用原生video标签 -->
          <div class="video-container" v-if="currentMedia.type === 'video'">
            <video
                :src="currentMedia.url"
                controls
                class="native-video-player"
                ref="videoElement"
            >
              您的浏览器不支持视频播放
            </video>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {computed, onMounted, ref} from 'vue';
import videoUrl from '@/assets/video/Recording.mp4';
// 模拟媒体数据 - 包含可播放的音视频URL
const mediaData = [
  {
    id: 1,
    title: "经典钢琴曲",
    author: "未知艺术家",
    duration: "2:45",
    type: "audio",
    thumbnail: "https://picsum.photos/id/1/300/300",
    url: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"
  },
  {
    id: 2,
    title: "吉他旋律",
    author: "未知艺术家",
    duration: "3:12",
    type: "audio",
    thumbnail: "https://picsum.photos/id/2/300/300",
    url: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3"
  },
  {
    id: 3,
    title: "电子音乐",
    author: "未知艺术家",
    duration: "4:05",
    type: "audio",
    thumbnail: "https://picsum.photos/id/3/300/300",
    url: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3"
  },
  {
    id: 4,
    title: "自然风景",
    author: "自然纪录片",
    duration: "0:30",
    type: "video",
    thumbnail: "https://picsum.photos/id/4/300/300",
    // url: "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_30mb.mp4"
    url: videoUrl
  },
  {
    id: 5,
    title: "城市延时",
    author: "城市影像",
    duration: "0:45",
    type: "video",
    thumbnail: "https://picsum.photos/id/5/300/300",
    url: "https://sample-videos.com/video123/mp4/720/sample_720x480.mp4"
  },
  {
    id: 6,
    title: "爵士音乐",
    author: "爵士乐队",
    duration: "3:45",
    type: "audio",
    thumbnail: "https://picsum.photos/id/6/300/300",
    url: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-4.mp3"
  },
  {
    id: 7,
    title: "动画短片",
    author: "动画工作室",
    duration: "1:20",
    type: "video",
    thumbnail: "https://picsum.photos/id/7/300/300",
    url: "https://sample-videos.com/video123/mp4/480/sample_480x270.mp4"
  }
];

// 状态管理
const currentPage = ref(1);
const itemsPerPage = ref(5);
const searchTerm = ref('');
const currentMediaType = ref('all');
const isPlayerOpen = ref(false);
const currentMedia = ref({});
const audioElement = ref(null);
const videoElement = ref(null);

// 过滤后的数据
const filteredData = computed(() => {
  return mediaData.filter(item => {
    const matchesSearch = searchTerm.value === '' ||
        item.title.toLowerCase().includes(searchTerm.value.toLowerCase()) ||
        item.author.toLowerCase().includes(searchTerm.value.toLowerCase());

    const matchesType = currentMediaType.value === 'all' || item.type === currentMediaType.value;

    return matchesSearch && matchesType;
  });
});

// 当前页显示的数据
const currentItems = computed(() => {
  const startIndex = (currentPage.value - 1) * itemsPerPage.value;
  const endIndex = startIndex + itemsPerPage.value;
  return filteredData.value.slice(startIndex, endIndex);
});

// 总页数
const totalPages = computed(() => {
  return Math.ceil(filteredData.value.length / itemsPerPage.value);
});

// 方法
const prevPage = () => {
  if (currentPage.value > 1) {
    currentPage.value--;
  }
};

const nextPage = () => {
  if (currentPage.value < totalPages.value) {
    currentPage.value++;
  }
};

const performSearch = () => {
  currentPage.value = 1; // 重置到第一页
};

const setMediaType = (type) => {
  currentMediaType.value = type;
  currentPage.value = 1; // 重置到第一页
};

const openPlayer = (item) => {
  currentMedia.value = item;
  isPlayerOpen.value = true;

  // 自动播放
  onMounted(() => {
    if (item.type === 'audio' && audioElement.value) {
      audioElement.value.play().catch(err => console.log('音频播放失败:', err));
    } else if (item.type === 'video' && videoElement.value) {
      videoElement.value.play().catch(err => console.log('视频播放失败:', err));
    }
  });
};

const closePlayer = () => {
  // 暂停播放
  if (currentMedia.value.type === 'audio' && audioElement.value) {
    audioElement.value.pause();
  } else if (currentMedia.value.type === 'video' && videoElement.value) {
    videoElement.value.pause();
  }

  isPlayerOpen.value = false;
};
</script>

<style>
/* 基础样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
}

/* 自定义图标样式 - 替代Font Awesome */
.icon-music::before {
  content: "♫";
}

.icon-search::before {
  content: "🔍";
}

.icon-video::before {
  content: "🎬";
}

.icon-prev::before {
  content: "←";
}

.icon-next::before {
  content: "→";
}

.icon-close::before {
  content: "✕";
}

body {
  background-color: #f5f5f5;
  color: #333;
  line-height: 1.6;
}

/* 顶部导航栏 */
.navbar {
  background-color: #c62f2f;
  color: white;
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
  position: sticky;
  top: 0;
  z-index: 100;
}

.logo {
  font-size: 24px;
  font-weight: bold;
  display: flex;
  align-items: center;
}

.logo i {
  margin-right: 10px;
  font-size: 28px;
}

.nav-links {
  margin-left: auto;
  display: flex;
}

.nav-links a {
  color: white;
  text-decoration: none;
  margin-left: 25px;
  font-size: 15px;
  transition: opacity 0.3s;
}

.nav-links a:hover {
  opacity: 0.8;
}

.nav-links a.active {
  font-weight: bold;
  position: relative;
}

.nav-links a.active::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 0;
  width: 100%;
  height: 3px;
  background-color: white;
  border-radius: 3px;
}

/* 主容器 */
.container {
  max-width: 1200px;
  margin: 30px auto;
  padding: 0 20px;
}

/* 搜索区域 */
.search-container {
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  margin-bottom: 30px;
}

.search-box {
  display: flex;
  max-width: 600px;
  margin: 0 auto;
}

.search-input {
  flex: 1;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-right: none;
  border-radius: 4px 0 0 4px;
  font-size: 15px;
  outline: none;
  transition: border-color 0.3s;
}

.search-input:focus {
  border-color: #c62f2f;
}

.search-button {
  background-color: #c62f2f;
  color: white;
  border: none;
  padding: 0 20px;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
  transition: background-color 0.3s;
  font-size: 18px;
}

.search-button:hover {
  background-color: #a82424;
}

.filter-tabs {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  gap: 10px;
}

.filter-tab {
  padding: 6px 15px;
  border-radius: 20px;
  background-color: #f5f5f5;
  border: none;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 14px;
}

.filter-tab.active {
  background-color: #c62f2f;
  color: white;
}

.filter-tab:hover:not(.active) {
  background-color: #e5e5e5;
}

/* 列表区域 */
.content-title {
  margin-bottom: 20px;
  font-size: 22px;
  color: #333;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.media-list {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.list-header, .list-item {
  display: grid;
  grid-template-columns:
    [number] 80px
    [title] 1fr
    [duration] 120px
    [type] 120px;
  width: 100%;
  padding: 12px 20px;
  align-items: center;
}

.list-header {
  background-color: #f9f9f9;
  font-weight: bold;
  color: #666;
  font-size: 14px;
  border-bottom: 1px solid #eee;
}

.list-item {
  border-bottom: 1px solid #eee;
  transition: background-color 0.2s;
  cursor: pointer;
  padding: 15px 20px;
}

.list-item:hover {
  background-color: #f9f9f9;
}

.list-item:last-child {
  border-bottom: none;
}

/* 列样式 - 确保对齐 */
.col-number {
  text-align: center;
}

.col-title {
  text-align: left;
}

.col-duration, .col-type {
  text-align: center;
}

.item-number {
  color: #999;
  font-size: 14px;
}

.item-number.top {
  color: #c62f2f;
}

.item-info {
  display: flex;
  align-items: center;
}

.media-thumbnail {
  width: 50px;
  height: 50px;
  object-fit: cover;
  margin-right: 15px;
  border-radius: 4px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  flex-shrink: 0; /* 防止缩略图被压缩 */
}

.media-details {
  overflow: hidden;
  flex-grow: 1;
}

.media-title {
  font-weight: 500;
  margin-bottom: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.media-author {
  color: #999;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item-duration {
  color: #999;
  font-size: 14px;
}

.item-type {
  color: #666;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.item-type.audio {
  color: #2196f3;
}

.item-type.video {
  color: #4caf50;
}

/* 分页控件 */
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 30px;
  align-items: center;
}

.page-button {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid #ddd;
  background-color: white;
  margin: 0 5px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.page-button:hover:not(.active):not(.disabled) {
  border-color: #c62f2f;
  color: #c62f2f;
}

.page-button.active {
  background-color: #c62f2f;
  color: white;
  border-color: #c62f2f;
}

.page-button.disabled {
  color: #ccc;
  cursor: not-allowed;
}

.page-info {
  margin: 0 15px;
  color: #666;
  font-size: 14px;
}

/* 播放页样式 */
.player-container {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.9);
  z-index: 1000;
  padding: 50px;
  color: white;
}

.player-container.active {
  display: block;
}

.close-player {
  position: absolute;
  top: 20px;
  right: 20px;
  background: none;
  border: none;
  color: white;
  font-size: 24px;
  cursor: pointer;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.2s;
}

.close-player:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.player-content {
  max-width: 1000px;
  margin: 0 auto;
}

.player-header {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
}

.player-thumbnail {
  width: 120px;
  height: 120px;
  object-fit: cover;
  margin-right: 20px;
  border-radius: 8px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}

.player-info h2 {
  font-size: 28px;
  margin-bottom: 10px;
}

.player-info p {
  color: #ccc;
  font-size: 16px;
}

/* 原生音视频播放器样式 */
.native-audio-player {
  width: 100%;
  margin-top: 20px;
  background-color: rgba(255, 255, 255, 0.1);
  padding: 10px;
  border-radius: 8px;
}

.native-video-player {
  width: 100%;
  height: auto;
  min-height: 400px;
  background-color: #000;
  border-radius: 8px;
}

.video-container {
  width: 100%;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .list-header {
    display: none;
  }

  .list-item {
    grid-template-columns:
      [number] 50px
      [title] 1fr
      [duration] 80px;
    padding: 10px 15px;
  }

  .col-type {
    display: none;
  }

  .media-thumbnail {
    width: 40px;
    height: 40px;
  }

  .nav-links a {
    margin-left: 15px;
    font-size: 14px;
  }

  .player-container {
    padding: 20px;
  }

  .player-header {
    flex-direction: column;
    text-align: center;
  }

  .player-thumbnail {
    margin-right: 0;
    margin-bottom: 20px;
  }

  .native-video-player {
    min-height: 250px;
  }
}
</style>
